package com.zzc.service;

import java.io.IOException;
import java.util.List;

import com.zzc.bean.Employee;
import com.google.gson.Gson;
import com.zzc.dao.DiningDao;
import com.zzc.dao.EmployeeDao;
import com.zzc.dao.DiningReceiptDao;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class EmployeeService {
	private EmployeeDao employeeDao;
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}
	private DiningReceiptDao diningReceiptDao;
	public void setDiningReceiptDao(DiningReceiptDao diningReceiptDao) {
		this.diningReceiptDao = diningReceiptDao;
	}
	private DiningDao diningDao;

	public void setDiningDao(DiningDao diningDao) {
		this.diningDao = diningDao;
	}

	public void init(){
		Employee admin =new Employee("管理员","admin","000000","青山控股集团","初始化员工","系统","系统");
		Employee employee1 =new Employee("陈之和","14480010101","000000","青山控股集团","初始化员工","系统","系统");
		Employee employee2 =new Employee("江侬鲁","14480010102","000000","瑞普科技有限公司","初始化员工","系统","系统");
		Employee employee3 =new Employee("姜利黎","14480010103","000000","青山控股集团","初始化员工","系统","系统");
		Employee employee4 =new Employee("李朝霞","14480010104","000000","青山钢铁有限公司","初始化员工","系统","系统");
		Employee employee5 =new Employee("李书易","14480010105","000000","青山控股集团","初始化员工","系统","系统");

		employeeDao.getSession().saveOrUpdate(admin);
		employeeDao.getSession().saveOrUpdate(employee1);
		employeeDao.getSession().saveOrUpdate(employee2);
		employeeDao.getSession().saveOrUpdate(employee3);
		employeeDao.getSession().saveOrUpdate(employee4);
		employeeDao.getSession().saveOrUpdate(employee5);
	}
	private int employee_id;//用户名称
	private String employee_name;//用户名称
	private String employee_account;//用户编号
	private String employee_password;//密码
	private String employee_company;//公司名称
	private String employee_note;//备注
	private String employee_founder;//创建人
	private String employee_modifier;//修改人
	public void addEmployee(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		employee_name=  request.getParameter("employee_name");
		employee_account=  request.getParameter("employee_account");
		employee_password=  request.getParameter("employee_password");
		employee_company=  request.getParameter("employee_company");
		employee_note=  request.getParameter("employee_note");
		employee_founder=  request.getParameter("employee_founder");
		employee_modifier=  request.getParameter("employee_modifier");
		Employee employee =new Employee(employee_name,employee_account,employee_password,employee_company,employee_note,employee_founder,employee_modifier);
		employeeDao.getSession().saveOrUpdate(employee);
	}
	public void delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String employee_account=request.getParameter("employee_account");
		if (employee_account==null||employee_account.equals("")){
			System.out.println("employee_account:null");
		}
		else {
			System.out.println("删除"+employee_account);
			Employee employee = new Employee();
			employee.setEmployee_account(employee_account);
			employeeDao.getSession().delete(employee);
		}
	}
	public void alter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		employee_id= Integer.parseInt(request.getParameter("employee_id"));
		employee_account=  request.getParameter("employee_account");
		Employee employee_alter = (Employee)employeeDao.getSession().get(Employee.class, employee_account);
		employee_name=  request.getParameter("employee_name");
		employee_password=  request.getParameter("employee_password");
		employee_company=  request.getParameter("employee_company");
		employee_note=  request.getParameter("employee_note");
		employee_modifier=  request.getParameter("employee_modifier");
		employee_alter.setEmployee_name(employee_name);
		employee_alter.setEmployee_account(employee_account);
		employee_alter.setEmployee_password(employee_password);
		employee_alter.setEmployee_company(employee_company);
		employee_alter.setEmployee_note(employee_note);
		employee_alter.setEmployee_modifier(employee_modifier);
		employeeDao.getSession().saveOrUpdate(employee_alter);
	}
	public Employee getEmployee(String account){
		return employeeDao.getEmployee(account);
	}
	public void get_id() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String string_employee_id=request.getParameter("employee_id");
		Employee employee=new Employee();
		if (string_employee_id==null||string_employee_id.equals("")){
			System.out.println("employee_id:null");
		}
		else {
			System.out.println("查询"+string_employee_id);
			employee_id= Integer.parseInt(string_employee_id);
			employee=employeeDao.get_id(employee_id);
		}
		BaseService.printAll(new Gson().toJson(employee));
	}
	public List<Employee> getAll(){
		return employeeDao.getAll();
	}
	public String login() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String employee_account=request.getParameter("employee_account");
		String employee_password=request.getParameter("employee_password");
		System.out.println(employee_account);
		System.out.println(employee_password);
		Employee employee = employeeDao.login(employee_account,employee_password);
		if (employee ==null|| employee.getEmployee_account().equals("")){
//			System.out.println("注册用户");
//			employee=new Employee();
//			employee.setAccount(account);
//			employeeDao.getSession().save(employeeDao);
			return "login_error";
		}
		else {
			System.out.println(employee.getEmployee_name()+"登录成功");
			session.setAttribute("employee", employee);
			if(employee.getEmployee_account().equals("admin")){
				return "admin";

			}
			else {
				return "employee";
			}
		}
	}


	public Employee alter_get_account() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String employee_account=request.getParameter("employee_account");
		Employee employee=new Employee();
		if (employee_account==null||employee_account.equals("")){
			System.out.println("employee_account:null");
		}
		else {
			employee=employeeDao.get_account(employee_account);
		}
		return employee;
	}

	public String login_employee(Employee employee_login) {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		Employee employee = employeeDao.login(employee_login.getEmployee_account(),employee_login.getEmployee_password());
		if (employee ==null|| employee.getEmployee_account().equals("")){
			return "login_error";
		}
		else {
			System.out.println(employee.getEmployee_name()+"登录成功");
			session.setAttribute("employee", employee);
			if(employee.getEmployee_account().equals("admin")){
				return "admin";

			}
			else {
				return "employee";
			}
		}
	}

	public String checkUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String employee_account=request.getParameter("employee_account");
		String employee_password=request.getParameter("employee_password");
		System.out.println(employee_account);
		System.out.println(employee_password);
		Employee employee = employeeDao.login(employee_account,employee_password);
		if (employee ==null|| employee.getEmployee_account().equals("")){
			BaseService.printAll(200);
			return "login_error";
		}
		else {
			System.out.println(employee.getEmployee_name()+"登录成功");
			BaseService.printAll(100);//100-> "100"
			return "login_success";
		}
	}



//	public void employeeMessage() {
//		HttpServletRequest request = ServletActionContext.getRequest();
//		HttpSession session=request.getSession();
//		Employee employee = (Employee) session.getAttribute("employee");
//		List<DiningReceipt> list_diningReceipts = diningReceiptDao.getSCs_employeeName(employee.getEmployee_name());
//		request.setAttribute("list_diningReceipts", list_diningReceipts);
//		Map<String,String> map_xuefens= new HashMap<String, String>();
//		for (DiningReceipt diningReceipt : list_diningReceipts){
//			List<Dining> list_cours = diningDao.get_courseName(diningReceipt.getCourseName());
//			for (Dining dining : list_cours){
//				map_xuefens.put(dining.getName(), dining.getXuefen());
//			}
//		}
//		request.setAttribute("map_xuefens",map_xuefens);
////		return "index";
//	}
}
